package com.ruicar.afs.cloud.channel.mange.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.channel.mange.entity.ChannelDevelopRegion;
import com.ruicar.afs.cloud.channel.mange.enums.IsParentEnum;
import com.ruicar.afs.cloud.channel.mange.service.ChannelDevelopRegionService;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author xudongdong
 * @date 2020-06-02
 */
@RestController
@AllArgsConstructor
@RequestMapping("/developRegion")
@Api(value = "developRegion", description = "展业区域信息")
public class ChannelDevelopRegionController {

    private final ChannelDevelopRegionService developRegionService;

    @PostMapping("/getByParentId/{parentId}")
    @ApiOperation(value = "通过id获取")
    public IResponse<List<ChannelDevelopRegion>> getByParentId(@PathVariable String parentId) {
        return new IResponse<List<ChannelDevelopRegion>>().setData(setInfo(developRegionService.list(Wrappers.<ChannelDevelopRegion>query()
                .lambda().eq(ChannelDevelopRegion::getParentId, parentId).orderByAsc(ChannelDevelopRegion::getSortOrder))));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加")
    @SysLog("展业区域")
    @Transactional
    public IResponse<Boolean> add(@RequestBody ChannelDevelopRegion developRegion) {
        developRegion.setIsParent(IsParentEnum.NO.getValue());
        developRegionService.save(developRegion);
        ChannelDevelopRegion developRegions = developRegionService.getById(developRegion.getParentId());
        if (developRegions != null && IsParentEnum.NO.getValue().equals(developRegions.getIsParent())) {
            developRegions.setIsParent(IsParentEnum.YES.getValue());
            developRegionService.updateById(developRegions);
        }
        return new IResponse<Boolean>().setMsg("添加成功").setData(true);
    }

    @PostMapping("/edit")
    @ApiOperation(value = "编辑")
    @SysLog("编辑展业区域")
    @Transactional
    public IResponse<Boolean> edit(@RequestBody ChannelDevelopRegion developRegion) {
        developRegionService.updateById(developRegion);
        return new IResponse<Boolean>().setMsg("编辑成功").setData(true);
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    @SysLog("批量通过id删除展业区域")
    @Transactional
    public IResponse<Boolean> delByIds(@PathVariable String[] ids) {

        Arrays.stream(ids).forEach(id -> {
            developRegionService.remove(Wrappers.<ChannelDevelopRegion>update().lambda().eq(ChannelDevelopRegion::getParentId, id));
            developRegionService.removeById(id);
        });

        return new IResponse<Boolean>().setMsg("批量通过id删除数据成功").setData(true);
    }

    @PostMapping("/search")
    @ApiOperation(value = "区域名模糊搜索")
    public IResponse<List<ChannelDevelopRegion>> searchByTitle(@RequestParam String title) {
        List<ChannelDevelopRegion> list = developRegionService.list(Wrappers.<ChannelDevelopRegion>query().lambda().
                like(ChannelDevelopRegion::getTitle, title).orderByAsc(ChannelDevelopRegion::getSortOrder));
        list = setInfo(list);
        return new IResponse<List<ChannelDevelopRegion>>().setData(list);
    }

    public List<ChannelDevelopRegion> setInfo(List<ChannelDevelopRegion> list) {

        // lambda表达式
        list.forEach(item -> {
            if (!CommonConstants.PARENT_ID.equals(Long.valueOf(item.getParentId()))) {
                ChannelDevelopRegion parent = developRegionService.getById(item.getParentId());
                item.setParentTitle(parent.getTitle());
            } else {
                item.setParentTitle("一级部门");
            }
        });
        return list;
    }
}
